use core::ops::Deref;

use cmsis_rtos2_rs::static_mem::{Task, Thread};
use utils::{log, ByteBuffer};

use crate::{address::MY, conn, driver::{cmd_can, cmd_uart}};


struct RecvTask;
static THREAD: Thread<2048> = Thread::uninit();
static REQUEST_BUF: ByteBuffer<255> = ByteBuffer::const_new();

impl Task for RecvTask {
    
    fn run(&self) {
        let buf = unsafe { REQUEST_BUF.as_mut_slice() };
        loop {
            let msg = match cmd_uart::read_message(buf) {
                Ok(msg) => msg,
                Err(e) => {
                    log!("recv error:{:?}", e);
                    continue;
                }
            };
            let dst = msg.dst();
            if dst != MY {
                cmd_can::write(msg.deref());
                continue;
            }
            conn::dispath(msg);
        }
    }
}

pub unsafe fn start() {
    THREAD.spawn(&RecvTask);
}








